package org.gruposp2p.proyectokarma.controller

import grails.converters.JSON
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.Authentication;
import org.gruposp2p.proyectokarma.domain.Peer;

class LoginSuccessController {

    def index = {
		SecurityContext securityContext = session.getAttribute("SPRING_SECURITY_CONTEXT");
		if (securityContext)  {
			Authentication authentication = securityContext.getAuthentication()
			log.debug " authentication.getPrincipal() : " + authentication.getPrincipal() 
			def userDetails = authentication.getPrincipal()
			log.debug " userDetails : " + userDetails;
			def user = Peer.get(userDetails.id).collect { peer ->
				return [id: peer.id, email: peer.email, username:peer.username]
			}
			log.debug " peer : " + user;
			response.status = 200;
			//response.setHeader('Location', CH.config.grails.serverURL + '/peers/' + peer.id)
			render  user as JSON
			return
		}
		response.status = 404;
        render "Page not found"
    }
    

}
